package gl.android.biyiqing.fragment;

import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.bean.BYQUserInfo;
import gl.common.AjaxCallBackProxy;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.LocationManagerUtil;
import gl.uitls.PointD;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class NearPeopleFragment extends BaseListFragmet {
	// Content View Elements

	// Content View Elements

	// Content View Elements

	private TextView empty;
	private ListView list;
	String NOTFOUND = "附近似乎\n没有什么人";
	private Drawable drawable;

	private void bindViews() {

		empty = (TextView) findViewById(R.id.empty);
		list = (ListView) findViewById(android.R.id.list);
		list.setEmptyView(empty);
		empty.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				initdata();
			}
		});

		empty.setText(NOTFOUND);

		empty.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
				null);
	}

	public static NearPeopleFragment newInstance() {
		NearPeopleFragment fragment = new NearPeopleFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	public NearPeopleFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.listview_empty, container, false);
		drawable = getActivity().getResources().getDrawable(
				R.drawable.ic_discovery_pic_btn2);
		bindViews();
		initdata();
		sendShark();
		return rootView;
	}

	private void initdata() {
		// {
		// jobs: 0,
		// username: "明月",
		// height: 178,
		// age: "0026",
		// userid: 162001,
		// groupid: 2,
		// nearby: 2.1232517843847,
		// education: 4,
		// avatar:
		// "data/attachment/avatar/201301/22/162001/avatar_big.jpg.thumb.jpg",
		// online: 1
		// }
		initAbsListView(getListView(), API.URL + "GetNearbyFor5?userid=%s",
				new Object[] { UserManger.getInstance().userid },
				R.layout.item_list_nearby, new String[] { "username", "avatar","nearby",
						BYQUserInfo.KEY_SHENFEN ,BYQUserInfo.KEY_GROUP,BYQUserInfo.KEY_VIP_UNIT }, new int[] { R.id.tv_nick, R.id.iv_avator,R.id.nearby,
						R.id.age,R.id.iv_vip,R.id.imageView1, R.id.iv_fav, R.id.iv_sendmsg });

	}

	private void sendShark() {
		PointD mPointD = LocationManagerUtil.localPostion(getActivity());
		BaseControl.get(API.URL + "SetNearby?userid=%s&lon=%s&lat=%s",
				new AjaxCallBackProxy() {
					@Override
					public void onSuccess(String t) {
						if (!isValueFragment()) {
							return;
						}
						super.onSuccess(t);
						initdata();
					}
				}, new Object[] { UserManger.getInstance().userid, mPointD.longitude,
						mPointD.latitude });
		// FIXME 上传自身位置
	}

	@Override
	protected String preFormatJsonData(String t) {
		System.out.println("preFormatJsonData:shenfen");
		return BYQUserInfo.preFormatShenFen(t);
	}
}